package zy.blue7.im.gui;

import io.netty.channel.Channel;
import zy.blue7.im.Client;
import zy.blue7.im.ClientContainer;
import zy.blue7.im.common.message.LoginMessage;

import javax.swing.*;
import java.awt.*;

/**
 * @author blue7
 * @version 1.0
 * @date 2022/3/2 9:16
 */
public class LoginFrame {

    private static JFrame login = new JFrame("登录页面");
    public static Channel clientChannel = null;

    public static void loginPage() {


        // 2.设置关闭窗口就是关闭程序
        login.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 最精准的布局模式空布局
        login.setSize(1000, 800);
        login.setLocation(500, 200);

        JPanel jPanel = new JPanel();
//        jPanel.setBackground(Color.blue); //设置背景色
        jPanel.setLayout(null);

        /**
         * --------------------------------------------------用户名组件------------------------------------------------------------------------------------------------
         */
        JPanel userNamePanel = new JPanel();
//        userNamePanel.setBackground(Color.CYAN);
        userNamePanel.setSize(600, 80);
        userNamePanel.setLocation(200, 200);
        userNamePanel.setLayout(null);

        JLabel userNameLabel = new JLabel("用户名");
        userNameLabel.setFont(new Font("宋体", Font.BOLD, 30));
        userNameLabel.setSize(100, 80);
        userNameLabel.setLocation(20, 0);
        /**
         * 给JLabel 添加背景色时，由于默认未透明色，必须先设置不透明
         */
        userNameLabel.setOpaque(true);
//        userNameLabel.setBackground(Color.YELLOW);
        userNamePanel.add(userNameLabel);
        jPanel.add(userNamePanel);

        JTextField userName = new JTextField();
        userName.setFont(new Font("宋体", Font.BOLD, 30));
        userName.setSize(440, 80);
        userName.setLocation(140, 0);
        userNamePanel.add(userName);


        /**
         * ----------------------------------------------------密码组件----------------------------------------------------------------------------------------------
         */
        JPanel passwordPanel = new JPanel();
//        passwordPanel.setBackground(Color.CYAN);
        passwordPanel.setSize(600, 80);
        passwordPanel.setLocation(200, 300);
        passwordPanel.setLayout(null);

        JLabel passwordLabel = new JLabel("密  码");
        passwordLabel.setFont(new Font("宋体", Font.BOLD, 30));
        passwordLabel.setSize(100, 80);
        passwordLabel.setLocation(20, 0);
        /**
         * 给JLabel 添加背景色时，由于默认未透明色，必须先设置不透明
         */
        passwordLabel.setOpaque(true);
//        passwordLabel.setBackground(Color.YELLOW);
        passwordPanel.add(passwordLabel);
        jPanel.add(passwordPanel);

        JPasswordField password = new JPasswordField();
        password.setFont(new Font("宋体", Font.BOLD, 30));
        password.setSize(440, 80);
        password.setLocation(140, 0);
        passwordPanel.add(password);

        /**
         * ----------------------------------------------------登录按钮----------------------------------------------------------------------------------------------------
         */
        JPanel loginPanel = new JPanel();
//        loginPanel.setBackground(Color.CYAN);
        loginPanel.setSize(600, 80);
        loginPanel.setLocation(200, 440);
        jPanel.add(loginPanel);

        JButton loginButton = new JButton("登录");
        loginButton.setFont(new Font("宋体", Font.BOLD, 30));
        /**
         * 无效的
         * https://blog.csdn.net/a8388011/article/details/79221874?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1.pc_relevant_paycolumn_v3&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1.pc_relevant_paycolumn_v3&utm_relevant_index=1
         */
//        loginButton.setSize(300,80);
        loginButton.setPreferredSize(new Dimension(300, 70));
        loginPanel.add(loginButton);


        login.add(jPanel);
        login.setVisible(true);


        /**
         * -----------------------------------------------------事件处理------------------------------------------------------------------------------------------------------------
         */
        loginButton.addActionListener(e -> {
            String userNameText = userName.getText();
            String passwordText = String.valueOf(password.getPassword());

            System.out.println("用户名： " + userNameText);
            System.out.println("密码： " + passwordText);

            LoginMessage loginMessage = new LoginMessage();
            loginMessage.setUsername(userNameText);
            loginMessage.setPassword(passwordText);
            loginMessage.setFrom(userNameText);

            /**
             * 保存登录状态,因为如果登录成功，从返回结果中获取不对登录人的信息，所以在这里 设置登陆人的信息，大不了 登上失败，也找不到这个信息（map.get   方法没有key）
             */
            ClientContainer.sessionMap.put(clientChannel, userNameText);

            clientChannel.writeAndFlush(loginMessage);

        });


    }

    public static void main(String[] args) throws InterruptedException {
//        loginPage();

        start();
    }


    public static void start() throws InterruptedException {
        clientChannel = Client.startClient();
        loginPage();
    }

    public static void close() {
        login.dispose();
    }

}
